package org.dtrd.modules.record.job;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.extern.slf4j.Slf4j;
import org.dtrd.common.util.DateUtils;
import org.dtrd.modules.achievement.entity.bean.SignInAction;
import org.dtrd.modules.achievement.entity.po.DtrdEntRdSigninRecord;
import org.dtrd.modules.achievement.service.IDtrdEntRdSigninRecordService;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * 漏打卡任务
 */
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
@Slf4j
@Component
public class MissRecordJob implements Job {

    @Autowired
    private IDtrdEntRdSigninRecordService signinRecordService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        LambdaQueryWrapper<DtrdEntRdSigninRecord> queryWrapper = Wrappers.lambdaQuery(DtrdEntRdSigninRecord.class)
                .eq(DtrdEntRdSigninRecord::getIsDel, 0)
                .eq(DtrdEntRdSigninRecord::getRecordDate, DateUtil.format(new Date(), DateUtils.date_sdf.get().toPattern()));
        List<DtrdEntRdSigninRecord> signinRecord = signinRecordService.list(queryWrapper);
        if (CollectionUtil.isEmpty(signinRecord)) {
            log.info("MissRecordJob.signinRecord.[] allSigninRecord is empty ");
            return;
        }
        log.info("MissRecordJob.signinRecord.[] allSigninRecord.size = " + signinRecord.size());
        signinRecord.forEach(p->{
            JSONArray jsonArray = JSONUtil.parseArray(p.getSighInJson());
            List<SignInAction> signInActionList = jsonArray.toList(SignInAction.class);
            // region 未打卡项发送提醒
            signinRecordService.sendMissRecordRemind(p.getPatientId(), signInActionList,p.getDataId());
            // endregion 未打卡项发送提醒
        });
    }
}